<?php
declare (strict_types=1);

namespace app\admin\controller\study;

use app\common\model\AdminAdmin;
use think\facade\Db;
use think\facade\Request;
use app\common\service\StudyStudentClass as S;
use app\common\model\StudyStudentClass as M;

class StudentClass extends \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck'];

    // 列表
    public function index()
    {
        return $this->getAuto($this->fetch(), M::getList());
    }

    // 添加
    public function add()
    {
        return $this->getAuto($this->fetch(), S::goAdd(Request::post()));
    }

    // 编辑
    public function edit($id)
    {
        return $this->getAuto($this->fetch('', ['model' => M::find($id)]), S::goEdit(Request::post(), $id));
    }

    // 状态
    public function status($id)
    {
        return $this->getJson(S::goStatus(Request::post('status'), $id));
    }

    // 删除
    public function remove($id)
    {
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 回收站
    public function recycle()
    {
        return $this->getAuto($this->fetch(), S::goRecycle());
    }

    /**
     * 分配老师
     * @param $id
     * @return mixed|object|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addTeacher($id)
    {
        $consultant_id = Db::table('bl_class_teacher')->where('class_id',$id)->column('teacher_id');
        $model = M::field('teacher,admin_id')->find($id);
//        if ($model['teacher']) {
//            $consultant_id = explode(',',$model['teacher']);
//        } else {
//            $consultant_id = [];
//        }
        if (session('admin.id')==1){
            $role = dataSelect('bl_admin_admin_role', [['role_id', '=', 6]], '*', 'id');
            $admin_id = [];
            $admin = session('admin');
            if ($role)
                foreach ($role as $v) {
                    $admin_id[] = $v['admin_id'];
                }
            $where = [];
            if ($admin['school_id']) {
                $where[] = ["school_id", "=", $admin['school_id']];
            }
            $consultant = AdminAdmin::order('id','desc')->field('id,nickname')->where($where)
                ->whereIn('id', $admin_id)
                ->select();
        }else{
            $where['top_id'] = $model['admin_id'];
            $consultant = AdminAdmin::where($where)->select();
        }
//        $consultant = AdminAdmin::select();
        foreach ($consultant as $k => $v) {
            if (in_array($v['id'], $consultant_id)) {
                $consultant[$k]['check'] = 'checked';
            } else {
                $consultant[$k]['check'] = '';
            }
        }
        return $this->getAuto($this->fetch('', ['id' => $id, 'teacher' => $consultant]), S::setStudent(Request::post(), $id));
    }

    public function getStudent()
    {
        $class = input('param.class_id');
        return $this->getAuto($this->fetch('', ['class_id' => $class]), M::getStudentList());
    }

}
